﻿using System;
using System.Drawing;

namespace ClockwiseClient
{
    public class ClassGraphInfor : ClassGraph
    {

        public override void Paint(Graphics g, Rectangle rect)
        {
            this.rect = rect;
            if (format.LineAlignment != StringAlignment.Center) format.LineAlignment = StringAlignment.Center;
            if (format.Alignment != StringAlignment.Center) format.Alignment = StringAlignment.Center;
            String tips = "显示区域太小了";
            stringSize = g.MeasureString(tips, font);
            if (rect.Width < 320 || rect.Height < 240)
            {
                if (stringSize.Width > rect.Width)
                {
                    tips = "太小了";
                    stringSize = g.MeasureString(tips, font);
                }
                if (stringSize.Width > rect.Width || stringSize.Height > rect.Height) return;
                PointF pointF = new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2 + stringSize.Height / 10);
                g.DrawString(tips, font, Brushes.Cyan, pointF, format);
                return;
            }
            ShowZiliao(g, rect);
        }

        public override EnumGraph GetGraphType() { return EnumGraph.GRAPH_INFOR; }

        void ShowZiliao(Graphics g, Rectangle rect)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            ClassFilter classFilter = classStockData.GetFilter();
            int idx = classStockData.focusStockIdx;
            if (classFilter != null) idx = classFilter.GetIndex(idx);
            string text;
            SizeF textSize;
            PointF pointF;
            ClassStockItem si = classStockData.GetClassStockItem(idx);
            me = si;
            if (si != null)
            {
                text = si.code;
                textSize = g.MeasureString(text, fontTitle1);
                format.Alignment = StringAlignment.Near;
                int left = rect.Left + 4;
                pointF = new PointF(left, rect.Top + 4 + textSize.Height / 2 + textSize.Height / 10);
                g.DrawString(text, fontTitle1, Brushes.Cyan, pointF, format);
                left += (int)textSize.Width;
                text = si.name;
                textSize = g.MeasureString(text, fontTitle1);
                pointF = new PointF(left, rect.Top + 4 + textSize.Height / 2 + textSize.Height / 10);
                g.DrawString(text, fontTitle1, classStockData.ZixuanguInclude(si.symbol) ? Brushes.Magenta : Brushes.Yellow, pointF, format);
            }
        }

        public override void MouseWheel(int delta)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            int idx = classStockData.boardStockIdx;
            if (idx >= 0)
            {
                if (delta < 0)
                {
                    if (idx < classStockData.GetCount() - 1) idx++; else idx = 0;
                }
                else
                {
                    if (idx > 0) idx--; else idx = classStockData.GetCount() - 1;
                }
                classStockData.boardStockIdx = idx;
                return;
            }
            ClassFilter classFilter = classStockData.GetFilter();
            idx = classStockData.focusStockIdx;
            if (delta < 0)
            {
                if (idx < classFilter.GetCount() - 1) idx++; else idx = 0;
            }
            else
            {
                if (idx > 0) idx--; else idx = classFilter.GetCount() - 1;
            }
            classStockData.focusStockIdx = idx;
        }

        public override bool KeyEsc() 
        { 
            return false; 
        }

    }
}
